#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Instrument.h>
#pragma unmanaged 
#include <ql\instruments\bond.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Instruments {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBond
	public ref class CBond : 
            public CInstrument,
            public Cephei::QL::Instruments::IBond
	{
	protected: 
		boost::shared_ptr<QuantLib::Bond>* _ppBond;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Bond>* _phBond;
#endif
		Object^ _BondOwner;     // reference to object that manages the storage for this object
	internal:
		CBond (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double faceAmount, DateTime maturityDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::ICashFlow^>^>^ cashflows, Cephei::QL::IPricingEngine^ QL_Pricer);
		CBond (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::ICashFlow^>^>^ coupons, Cephei::QL::IPricingEngine^ QL_Pricer);
        CBond (boost::shared_ptr<QuantLib::Bond>& childNative, Object^ owner);
        CBond (QuantLib::Bond& childNative, Object^ owner);
        CBond (CBond^ copy);
        CBond (System::Type^ t);
#ifdef STRUCT
        CBond (QuantLib::Bond childNative);
#endif       
#ifdef HANDLE
		CBond (QuantLib::Handle<QuantLib::Bond>& childNative, Object^ owner);
		CBond (QuantLib::Handle<QuantLib::Bond> childNative);
#endif
		virtual ~CBond ();
		!CBond ();

	internal:
		QuantLib::Bond& GetReference ();
		boost::shared_ptr<QuantLib::Bond>& GetShared ();
		QuantLib::Bond* GetPointer ();
        void SetBond (boost::shared_ptr<QuantLib::Bond> native)
        {
            if (_ppBond != NULL)
                delete _ppBond;
            _ppBond = new boost::shared_ptr<QuantLib::Bond> (native);
            SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppBond));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Bond>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double AccruedAmount (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
        property Cephei::QL::Times::ICalendar^ Calendar 
        {
		    virtual Cephei::QL::Times::ICalendar^ get () ;
        }
        property Cephei::IVector<Cephei::QL::ICashFlow^>^ Cashflows 
        {
		    virtual Cephei::IVector<Cephei::QL::ICashFlow^>^ get () ;
        }
		virtual Double CleanPrice (Double yield, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double CleanPrice () ;
		virtual Double DirtyPrice (Double yield, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double DirtyPrice () ;
        property Boolean IsExpired 
        {
		    virtual Boolean get () ;
        }
        property DateTime IssueDate 
        {
		    virtual DateTime get () ;
        }
        property DateTime MaturityDate 
        {
		    virtual DateTime get () ;
        }
		virtual Double Notional (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
		virtual Boolean IsTradable (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
		virtual DateTime NextCashFlowDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
		virtual Double NextCouponRate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
		virtual DateTime PreviousCashFlowDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
        property Cephei::IVector<Double>^ Notionals 
        {
		    virtual Cephei::IVector<Double>^ get () ;
        }
        property Cephei::QL::ICashFlow^ Redemption 
        {
		    virtual Cephei::QL::ICashFlow^ get () ;
        }
        property Cephei::IVector<Cephei::QL::ICashFlow^>^ Redemptions 
        {
		    virtual Cephei::IVector<Cephei::QL::ICashFlow^>^ get () ;
        }
        property UInt32 SettlementDays 
        {
		    virtual UInt32 get () ;
        }
		virtual Double SettlementValue (Double cleanPrice) ;
		virtual Double SettlementValue () ;
		virtual Double Yield (Double cleanPrice, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations) ;
		virtual Double Yield (Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations) ;
		virtual Double PreviousCouponRate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
		virtual DateTime SettlementDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CBond_Factory : public System::MarshalByRefObject,  public IBond_Factory
	{
	public:
        virtual IBond^ Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double faceAmount, DateTime maturityDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::ICashFlow^>^>^ cashflows, Cephei::QL::IPricingEngine^ QL_Pricer);
        virtual IBond^ Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Cephei::QL::ICashFlow^>^>^ coupons, Cephei::QL::IPricingEngine^ QL_Pricer);
    };
   
/*Cephei*/ } /*QL*/ } /*Instruments */}
